package com.example.wwk.graduationpractice.Activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wwk.graduationpractice.Adapter.AnswerAdapter;

import com.example.wwk.graduationpractice.Bean.AnswerBean;
import com.example.wwk.graduationpractice.Bean.QuestionBean;
import com.example.wwk.graduationpractice.NetDataUtils.DynamicDataUtils;
import com.example.wwk.graduationpractice.Globalutils.ActivityCollector;

import com.example.wwk.graduationpractice.Globalutils.CustomDialogUtils;
import com.example.wwk.graduationpractice.Globalutils.PromptShowUtils;
import com.example.wwk.graduationpractice.R;

import java.util.ArrayList;
import java.util.List;

public class AnswerActivity extends AppCompatActivity implements View.OnClickListener {
    //back
    private ImageView back;
    //显示从intent传过来的QuestionBean的字段值
    private String questionid;
    private TextView Question_Name;
    private TextView Question_Time;
    private TextView Question_Content;
    //列表数据以及网络数据的list
    private List<AnswerBean> answerBeanList = new ArrayList<>();
    private List<AnswerBean> answerBeen = new ArrayList<>();
    //recyclerView以及适配器
    private AnswerAdapter answerAdapter;
    private RecyclerView recyclerView;
    //刷新
    private Toolbar toolbar;
    //    private SwipeRefreshLayout swipeRefresh;
    private boolean refresh = false;
    //handler
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DynamicDataUtils.GET_ANSWER_SUCCESS:
                    answerBeen = (List<AnswerBean>) msg.obj; //转换从网络获得的数据的类型
                    initDatas(); //初始化问题列表的数据
                    answerAdapter.notifyDataSetChanged();
                    if (!refresh)
                        progressDialog.dismiss();


            }
        }
    };
    //加载对话框
    private CustomDialogUtils progressDialog;
    //intent传来的值
    private String thisuserName;
    private int identity;
    //log tag
    private String TAG = "123";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.answer_activity);
        setTitle("");
        ActivityCollector.addActivity(this);
        //toolbar
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //加载网络获得数据的进度条
        progressDialog = new CustomDialogUtils(AnswerActivity.this, R.style.CustomDialog);
        progressDialog.setCancelable(false);        //无法手动取消
        progressDialog.show();
        //intent
        Intent intent = getIntent();
        QuestionBean questionBean = intent.getParcelableExtra("questionBean");
        thisuserName = intent.getStringExtra("thisuserName");
        identity = intent.getIntExtra("identity", 0);
        //back
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(this);
        //init question
        Question_Name = (TextView) findViewById(R.id.quesion_name);
        Question_Time = (TextView) findViewById(R.id.quesion_time);
        Question_Content = (TextView) findViewById(R.id.quesion_content);
        initQuestion(questionBean);
        int questionID = questionBean.getQuestionId();
        questionid = String.valueOf(questionID);
        //获得该问题的评答列表的数据
        DynamicDataUtils.getAnwers(questionid, handler);
        //fab
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        if (identity == LoginActivity.USER)     //不同身份的控件显示区别
            fab.setVisibility(View.GONE);
        else
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(AnswerActivity.this, ReplyActivity.class);//点击跳转页面
                    intent.putExtra("thisuserName", thisuserName);
                    intent.putExtra("questionid", questionid);
                    Log.d(TAG, "questionid: " + questionid);
                    startActivityForResult(intent, 1);//1为请求码
                }
            });

        //RecyclerView
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        answerAdapter = new AnswerAdapter(answerBeanList, thisuserName, identity);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(answerAdapter);
//        //swipe刷新
//        swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
//        swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
//        swipeRefresh.setOnRefreshListener(new AnswerActivity.MyRefreshListener());


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    /*
    intent传值的返回结果数据
    * @param int
    * @param int
    * @param intent
    * return void
    */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {   //请求码
            case 1:
                if (resultCode == RESULT_OK) {// 接收数据
                    AnswerBean answerBean = data.getParcelableExtra("answerBean");
                    updateList(answerBean);
                }
                break;
        }
    }

    /*
    更新列表的显示数据
    * @param AnswerBean
    * return void
    */
    private void updateList(AnswerBean answerBean) {
        answerBeanList.add(0, answerBean);       //添加置顶
        answerAdapter.notifyDataSetChanged();
        recyclerView.scrollToPosition(0);
    }

    /*
    * 初始化问题
    * @param QuestionBean
    * return void
    */
    private void initQuestion(QuestionBean questionBean) {
        Question_Name.setText(questionBean.getName());
        Question_Time.setText(questionBean.getTime());
        Question_Content.setText(questionBean.getContent());
    }

    /*
    swipeRefresh监听事件
    */
    class MyRefreshListener implements SwipeRefreshLayout.OnRefreshListener {
        @Override
        public void onRefresh() {
            refreshList();
        }
    }

    /*
    menu菜单
    */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.refresh:
                refresh = true;
                progressDialog = new CustomDialogUtils(AnswerActivity.this, R.style.CustomDialog);
                progressDialog.setCancelable(false);        //无法手动取消
                progressDialog.show();
                refreshList();
                break;
        }
        return true;
    }

    /*
            * 列表刷新的处理
            */
    private void refreshList() {
        DynamicDataUtils.getAnwers(questionid, handler);    //网络请求数据
        initDatas();        //初始化数据
        runOnUiThread(new Runnable() {  //主线程更新UI
            @Override
            public void run() {
                answerAdapter.notifyDataSetChanged();
                new Handler().postDelayed(new Runnable() {     //Handler实现延迟刷新UI
                    @Override
                    public void run() {
//                        swipeRefresh.setRefreshing(false);
                        progressDialog.dismiss();
                        refresh = false;
                    }
                }, 2000);        //秒

            }
        });

    }

    /*
    * 初始化answerBeanList
    */
    private void initDatas() {
        answerBeanList.clear();
        for (AnswerBean answerBean : answerBeen) {
            answerBeanList.add(answerBean);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
